//
//  BaseNavigationController.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/4/26.
//

import UIKit

class BaseNavigationController: UINavigationController {
    private var pushing : Bool = false
    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self
        self.interactivePopGestureRecognizer?.isEnabled = true
        self.setNaviBackground()
    }
    public func setNaviBackground(_ selectColor :UIColor = UIColor.white){
        let color = UIColor(hex: "ffffff")
        let naviBar = self.navigationBar
        naviBar.setBackgroundImage(UIImage.imageWithColor(color: color), for: .default)
        naviBar.shadowImage = UIImage.imageWithColor(color: color)
        naviBar.titleTextAttributes = defaultNvi()
        naviBar.largeTitleTextAttributes = defaultLargeNvi()
        naviBar.isTranslucent = false
        ///MARK:切换皮肤会失败
        if #available(iOS 15.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundEffect = UIBlurEffect(style:.regular)
            appearance.shadowColor = color
            appearance.backgroundImage = UIImage.imageWithColor(color: color)
            appearance.backgroundColor = color
            appearance.titleTextAttributes = defaultNvi()
            appearance.largeTitleTextAttributes = defaultLargeNvi()
            naviBar.scrollEdgeAppearance = appearance
            naviBar.standardAppearance = appearance
            naviBar.compactAppearance = appearance
            naviBar.compactScrollEdgeAppearance = appearance
        } else {
            // Fallback on earlier versions
        }
        naviBar.prefersLargeTitles = true
        self.navigationItem.largeTitleDisplayMode = .automatic
    }
    private func defaultNvi()-> [NSAttributedString.Key : Any]{
        let color = UIColor(hex:"000000")
        var dic :[NSAttributedString.Key : Any] = [ : ]
        dic.updateValue(UIFont.systemFont(ofSize: 18, weight: UIFont.Weight(rawValue: 500)), forKey: .font)
        dic.updateValue(color, forKey: .foregroundColor)
        return dic
    }
    private func defaultLargeNvi()-> [NSAttributedString.Key : Any]{
        let color = UIColor(hex: "000000")
        var dic :[NSAttributedString.Key : Any] = [ : ]
        dic.updateValue(UIFont.systemFont(ofSize: 30, weight: UIFont.Weight(rawValue: 700)), forKey: .font)
        dic.updateValue(color, forKey: .foregroundColor)
        return dic
    }
    override var prefersStatusBarHidden: Bool{
        guard let visibleViewController = self.visibleViewController else { return false }
        return visibleViewController.prefersStatusBarHidden
    }
    override var preferredStatusBarStyle: UIStatusBarStyle{
        guard let visibleViewController = self.visibleViewController else { return .default }
        return visibleViewController.preferredStatusBarStyle
    }
    override var shouldAutorotate: Bool{
        guard let visibleViewController = self.visibleViewController else { return false }
        return visibleViewController.shouldAutorotate;
    }
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        guard let visibleViewController = self.visibleViewController else { return .portrait }
        return visibleViewController.supportedInterfaceOrientations
    }
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
        guard let visibleViewController = self.visibleViewController else { return .portrait }
        return visibleViewController.preferredInterfaceOrientationForPresentation
    }
}
extension BaseNavigationController :UINavigationControllerDelegate{
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.pushing == true {
            return;
        }else{
            self.pushing = true;
        }
        super.pushViewController(viewController, animated: animated);
    }
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        self.pushing = false;
    }
}
